(function(KKBus){

    function ExchangeController() {
        var self = this;
           
      self.exchange = ko.observableArray();
      
      self.showGifts = function (index)
        {
            self.exchange.removeAll();
            app.request++;
            
            var user = app.Knockout.UserController.user()[index];
            var userId = user.id();
            
            var id = userId;    
            console.log(userId);
            
            var url = 'rest/exchange/user/'+id;
            app.request++;
        
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
           
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
        
            while(i--){
                 self.exchange.push(new KKBus.ExchangeModel(jsData[i].userId, jsData[i].awardId, jsData[i].hash, jsData[i].awardName,jsData[i].id)); 
         }
         
         app.request--;
         
         if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + ExchangeController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
  
        
                  
             self.deleteExchange = function(id) {
                KKBus.Ajax.doDelete('rest/exchange/'+id, function(result, textStatus, jqXHR) {
            });   
        };    
              

        
        
        
    }
   

    KKBus.ExchangeController = ExchangeController;
    
}(window.KKBus));


